#include "support.h"
#include <stdio.h>

#define ENCODE(c) ((c) ? ((c) & 077) + ' ': '`')

int uuencode(const char *name, const char *inFileName, FILE *fpOut, short mode)
{
   char buf[80];
   int i, n;
   FILE *fpIn;

   fpIn = fopen(inFileName, "r");
   if(!fpIn)
       return 0;

   fprintf(fpOut, "begin %o %s\n", mode, name);
   while(1)
   {
      n = fread(buf, 1, 45, fpIn);

      putc(ENCODE(n), fpOut);
      for (i=0; i<n; i += 3)
      {
	 fputc(ENCODE(buf[i] >> 2), fpOut);
	 fputc(ENCODE(((buf[i] << 4) & 060) | ((buf[i + 1] >> 4) & 017)), fpOut);
	 fputc(ENCODE(((buf[i + 1] << 2) & 074) | ((buf[i + 2] >> 6) & 03)), fpOut);
	 fputc(ENCODE(buf[i + 2] & 077), fpOut);
      }
      putc('\n', fpOut);
      if (n <= 0)
	  break;
   }
   fputs("end\n", fpOut);
   fclose(fpIn);
   fflush(fpOut);
   return 1;
}

/*
 * to64 -- a filter from ascii to base64 encoding, from the
 *      metamail distribution.
 */
/*
Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)

Permission to use, copy, modify, and distribute this material 
for any purpose and without fee is hereby granted, provided 
that the above copyright notice and this permission notice 
appear in all copies, and that the name of Bellcore not be 
used in advertising or publicity pertaining to this 
material without the specific, prior written permission 
of an authorized representative of Bellcore.  BELLCORE 
MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
*/

static char basis_64[] =
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


void output64chunk(int c1, int c2, int c3, int pads, FILE *outfile)
{
    (void) putc(basis_64[c1>>2], outfile);
    (void) putc(basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
    if (pads == 2) {
        (void) putc('=', outfile);
        (void) putc('=', outfile);
    } else if (pads) {
        (void) putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
        (void) putc('=', outfile);
    } else {
        (void) putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
        (void) putc(basis_64[c3 & 0x3F], outfile);
    }
}

void to64(FILE *infile, FILE *outfile) 
{
    int c1, c2, c3, ct=0;
    while ((c1 = getc(infile)) != EOF) {
        c2 = getc(infile);
        if (c2 == EOF) {
            output64chunk(c1, 0, 0, 2, outfile);
        } else {
            c3 = getc(infile);
            if (c3 == EOF) {
                output64chunk(c1, c2, 0, 1, outfile);
            } else {
                output64chunk(c1, c2, c3, 0, outfile);
            }
        }
        ct += 4;
        if (ct > 71) {
            (void) putc('\n', outfile);
            ct = 0;
        }
    }
    if (ct) (void) putc('\n', outfile);
    (void) fflush(outfile);
}

/* End of Bellcore material. */

int base64(const char *inFileName, FILE *fp)
{
   FILE *fpIn;

   fpIn = fopen(inFileName, "r");
   if(!fpIn)
       return 0;
   to64(fpIn, fp);
   fclose(fpIn);
   return 1;
}
